;;; +hanh.el --- hanh-ngyoq input & translate -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

;; ;; -------- Go Translate --------
;; This is a translation framework on Emacs,
;; with high configurability and extensibility.
(use-package gt
  :defer t
  :ensure t
  :config
  (setq gt-default-translator
        (gt-translator
         :taker (list
                 (gt-taker :langs '(en zh)
                           :text 'word
                           :pick 'paragraph
                           :prompt nil)
                 (gt-taker :langs '(zh en)
                           :text 'selection
                           :pick 'paragraph
                           :prompt nil))
         :engines (list
                   (gt-stardict-engine
                    :dir (expand-file-name
                          "dicts/stardict-langdao-ec-gb-2.4.2" ;; 指定数据文件位置
                          user-emacs-directory)
                    :dict "朗道英汉字典5.0" ;; 可以指定具体使用的字典
                    :exact t) ;; do not fuzzy-search, only return exact matches
                   (gt-stardict-engine
                    :if 'selection
                    :dir (expand-file-name
                          "dicts/stardict-ghycyzzd-2.4.2" ;; 指定数据文件位置
                          user-emacs-directory)
                    :dict "古汉语常用字字典" ;; 可以指定具体使用的字典
                    :exact t)
                   (gt-bing-engine))
         :render (list
                  (gt-posframe-pop-render
                   ;; 通过 posframe 方式渲染
                   :frame-params (list :height 50
                                       :border-width 5))
                  (gt-alert-render
                   ;; 如果翻译的是只读 buffer 中的非单词，那么通过 alert 渲染
                   :if '(and read-only not-word))
                  (gt-buffer-render))))
  :init
  (if (equal (substring (user-login-name) 0 3) "cor")
      ;; 烏黑 #392f41 ;; 石青 #7bcfa6 ;; 煤黑 #261f1e ;; 滄浪 #b1d5c8
      (progn
        (setq gt-pop-posframe-forecolor "#7bcfa6")
        (setq gt-pop-posframe-backcolor "#392f41"))
    (progn
      (setq gt-pop-posframe-forecolor "#261f1e")
      (setq gt-pop-posframe-backcolor "#b1d5c8")))
  :bind
  (("C-c T" . 'gt-translate)))

;; -------- auto switch OS input method --------
(use-package sis
  :hook
  ((markdown-mode org-mode)
   . (lambda ()
       ;; enable the /follow context/ mode
       (sis-context-mode t)
       ;; enable the /inline english/ mode
       ;; 中文输入法状态下，中文后<spc>自动切换英文，结束后自动切回中文
       (sis-inline-mode t)))
  :init
  ;; `C-s/r' 默认优先使用英文，必须在 sis-global-respect-mode 前配置
  (setq sis-respect-go-english-triggers
        ;; isearch-forward 命令时默认进入
        (list 'isearch-forward 'isearch-backward)
        sis-respect-restore-triggers
        ;; isearch-forward 恢复, isearch-exit `<Enter>', isearch-abor `C-g'
        (list 'isearch-exit 'isearch-abort))
  (setq sis-prefix-override-keys (list "C-c" "C-x" "C-h" "<f1>" "<f10>" "C->"))
  (setq sis-default-cursor-color "DarkCyan" ;; 英文光标色
        sis-other-cursor-color "purple2") ;; 中文光标色
  ;; (setq
  ;;  ;; 删除头部空格，默认1，删除一个空格，1/0/'all
  ;;  sis-inline-tighten-head-rule 'zero
  ;;  ;; 删除尾部空格，默认1，删除一个空格，1/0/'all
  ;;  sis-inline-tighten-tail-rule 'zero
  ;;  ;; 默认是t, 中文context下输入<spc>进入内联英文
  ;;  sis-inline-with-english t
  ;;  ;; 默认是nil，而且prog-mode不建议开启, 英文context下输入<spc><spc>进行内联中文
  ;;  sis-inline-with-other t)
  :config
  (cond
   ((eq system-type 'gnu/linux)
    (cond
     ((executable-find "fcitx5")
      (sis-ism-lazyman-config "1" "2" 'fcitx5))
     ((executable-find "fcitx")
      (sis-ism-lazyman-config "1" "2" 'fcitx))))
   ((eq system-type 'windows-nt)
    (sis-ism-lazyman-config nil t 'w32)))
  (sis-global-cursor-color-mode t)
  ;; enable the /respect/ mode buffer 输入法状态记忆模式
  (sis-global-respect-mode t))

(provide '+hanh)

;;; +hanh.el ends here
